package zj.xinxin.web.controller.article;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import zj.xinxin.api.moudle.article.api.ArticleService;
import zj.xinxin.api.moudle.article.dto.ArticleDto;
import zj.xinxin.api.moudle.article.vo.ArticlePageSearchVo;
import zj.xinxin.api.moudle.article.vo.ArticleUpdateVo;
import zj.xinxin.api.moudle.article.vo.ArticleVo;
import zj.xinxin.common.result.PageResult;
import zj.xinxin.common.result.ResponseResult;
import zj.xinxin.common.utils.FileUtil;

import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 文章  前端控制器
 * </p>
 *
 * @author zhoujun
 * @since 2022-04-12
 */
@RestController
@Api(tags = "文章信息")
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    //上传md文件
    @PostMapping("/add")
    @ApiOperation(value = "保存文章信息")
    public ResponseResult<Boolean> add(@RequestBody @Valid ArticleVo article) {

        return articleService.addArticle(article) ?
                ResponseResult.success("添加成功", true ):
                ResponseResult.fail("保存失败", false);
    }

    @GetMapping("/{articleId}")
    @ApiOperation(value = "查询文章详情")
    public ResponseResult<ArticleDto> getArticleInfo(@PathVariable("articleId") Long articleId){
        ArticleDto articleDto = articleService.getByArticleId(articleId);

        return Objects.isNull(articleDto) ?
                ResponseResult.fail("获取文章信息失败！", null) :
                ResponseResult.success("获取文章信息成功！ ",
                        articleService.getByArticleId(articleId));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询文章列表")
    public ResponseResult<PageResult<ArticleDto>> pageArticle(@Valid ArticlePageSearchVo searchVo){

        return ResponseResult.success(articleService.searchPage(searchVo));
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新文章详情")
    public ResponseResult<Boolean> update(@RequestBody @Valid ArticleUpdateVo article){

        return articleService.updateByVo(article) ?
                ResponseResult.success("更新文章信息成功！", true ):
                ResponseResult.fail("更新文章信息失败！", false);
    }
}

